package com.dirtylily.myblog.service;

import com.dirtylily.myblog.dao.BlogDao;
import com.dirtylily.myblog.dao.CommentDao;
import com.dirtylily.myblog.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service

public class CommentService {
    @Autowired
    CommentDao commentDao;
    @Autowired
    BlogDao blogDao;

    private List<Comment> tempReplys = new ArrayList<>();


    public List<Comment> getAllCommentById(Long blogId){
        List<Comment> comments = commentDao.findByBlogIdParentIdNull(blogId, Long.parseLong("-1"));
        for(Comment comment : comments){
            Long id = comment.getId();
            String parentNickname1 = comment.getNickname();
            List<Comment> childComments = commentDao.findByBlogIdParentIdNotNull(blogId,id);

            //查询出子评论
            combineChildren(blogId, childComments, comment);
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
        System.out.println(comments);
        return comments;
    }

    public int saveComment(Comment comment){
        comment.setCreateTime(new Date());
        int i = commentDao.saveComment(comment);
        return i;
    }
    /**
     * @Description: 查询出子评论
     * @Auther: ONESTAR
     * @Date: 10:43 2020/6/23
     * @Param: childComments：所有子评论
     * @Param: parentNickname1：父评论姓名
     * @Return:
     */
    private void combineChildren(Long blogId, List<Comment> childComments, Comment comment) {
        //判断是否有一级子评论
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                childComment.setParentComment(comment);
                String parentNickname = childComment.getNickname();
//                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getId();
                //查询出子二级评论
                recursively(blogId, childId, childComment);
            }
        }
    }

    /**
     * @Description: 循环迭代找出子集回复
     * @Auther: ONESTAR
     * @Date: 10:44 2020/6/23
     * @Param: chileId:子评论id
     * @Param: parentNickname1:子评论姓名
     * @Return:
     */
    private void recursively(Long blogId, Long childId, Comment comment) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentDao.findByBlogIdAndReplyId(blogId,childId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentComment(comment);
//                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplys.add(replayComment);
                recursively(blogId,replayId,replayComment);
            }
        }
    }

}
